package com.foreveross.crawl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.triggers.SimpleTriggerImpl;

import com.foreveross.crawl.workers.listener.DefaultCrawlWorkerListener;
import com.foreveross.crawl.workers.listener.DefaultCrawlWorkerListenerBeta1;


public class CrawlContext {
	
	protected static Logger logger = Logger.getLogger(CrawlEngineStartUp.class);
	/** Version of this source. */
    public static final String VERSION = "version 1.0";

	 /**
	  * 抓取任务常量标识
	  */
	 public static final String CRAWL_TASK = "CRAWL_TASK";
	 
	 
	 /**
	  * 任务分发组
	  */
	 public static final String CRAWL_TASK_DISPACHE_GROUP="CRAWL_TASK_DISPACHE_GROUP";
	 
	 /**
	  * 任务打包组
	  */
	 public static final String CRAWL_TASK_LINE_CLASSIFY_GROUP="CRAWL_TASK_LINE_CLASSIFY_GROUP";
	 
	 
	 
	 
	 
	 public static final String CRAWL_TASK_FROM="CRAWL_TASK_FROM";
	 public static final String CRAWL_TASK_TO="CRAWL_TASK_TO";
	 public static final String CRAWL_TASK_FLIGHTDATE="CRAWL_TASK_FLIGHTDATE";
	 public static final String CRAWL_TASK_CHANNEL_GROUP="CRAWL_TASK_CHANNEL_GROUP";
	 public static final String CRAWL_TASK_CHANNEL_BASE="CRAWL_TASK_CHANNEL_BASE";
	 public static final String CRAWL_TASK_COMPERSATION_SCAN="CRAWL_TASK_COMPERSATION_SCAN";
	 public static final String CRAWL_TASK_TIMEOUT="CRAWL_TASK_TIMEOUT";
	 
	 
	 /**
	  * 节点类型，1==主节点  2==一般节点 3==指定节点
	  * 
	  */
	 public static final int NODE_TYPE_1_MAIN=1;
	 public static final int NODE_TYPE_2_BASE=2;
	 public static final int NODE_TYPE_3_ASSIGN=3;
	 
	 static {
		 
	 }
	 /**
	  * 抓取渠道常量标识
	  */
	 public static final String CRAWL_CHANNEL = "CRAWL_CHANNEL";
	 
	 
	 /**
	  * 抓取渠道id标识
	  */
	 public static final String CRAWL_CHANNEL_ID = "CRAWL_CHANNEL_ID";
	 
	 /**
	  * 抓取任务锁对象
	  */
	 public static final String CRAWL_SERVICESLOCK = "CRAWL_SERVICESLOCK";
	 /**
	  * 任務補償抓取渠道常量标识
	  */
	 public static final String CRAWL_TASKCOMPENSATION_CHANNEL ="CRAWL_TASKCOMPENSATION_CHANNEL";
	 /**
	  * 任務補償抓取分发标识
	  */
	 public static final String CRAWL_TASKCOMPENSATION_GROUPNAME ="CRAWL_TASKCOMPENSATION_GROUPNAME";
	 
	 public static final String RAWL_TASKCOMPENSATION_TRIGGERGROUP = "RAWL_TASKCOMPENSATION_TRIGGERGROUP";
	 
	 public static final String CRAWL_DO_GROUPNAME = "DOTASK_GROUP_";
	 
	 public static final String CRAWL_TASKCOMPENSATION_JOBDETAIL_PREFIX = "CRAWL_ASKCOMPENSATION_JOBDETAIL_PREFIX";
	 
	 public static final String CRAWL_JOBDETAIL_PREFIX = "JOBDETAIL_";
	 
	 public final static String MonitorContainerAllCount = "crawlEngineMonitor-allDataCount";
	 /**
	  * 默认一次获得任务队列的数量
	  */
	 public static final int DEFAULT_TASK_SIZE = 10;
	 
	 
	 private static CrawlContext context = new CrawlContext();
	 
	 private Map<String , Object> cacheDatas = new HashMap<String, Object>();
	 
	 private Scheduler scheduler;
	 
	 private boolean startStatus = false;
	 
	 public boolean debug = false;
	
	 
	 
	 public static CrawlContext getInstance(){
		 return context;
	 }  
	 
	 private CrawlContext(){
		 try {
			scheduler = new StdSchedulerFactory().getScheduler();
			scheduler.getListenerManager().addSchedulerListener(new TaskMonitorSchedulerListener());
//			scheduler.addJobListener(new DefaultCrawlWorkerListener());
			//TODO 新版本监听
//			scheduler.getListenerManager().addJobListener(new DefaultCrawlWorkerListenerBeta1());
			logger.info("Quartz启动......");
			scheduler.start();
			startStatus = true;
		} catch (SchedulerException e) {
			logger.error("Quartz启动失败!......");
			System.exit(0);
		}
	 }
	 
	 
	/**
	 * 获得一个立即启动的只运行一次的trigger
	 * @return
	 */
	public  SimpleTrigger getImmediatelyOnceTrigger() {
		return new SimpleTriggerImpl("ImmediatelyOnceTriggerGroup"+UUID.randomUUID().toString().replaceAll("-", ""), 
				"ImmediatelyOnceTriggerGroup",new Date(),null,0,0l);
	}

	public Scheduler getScheduler() {
		return scheduler;
	}

	public boolean isStartStatus() {
		return startStatus;
	}

	public Map<String, Object> getCacheDatas() {
		return cacheDatas;
	}

	public boolean isDebug() {
		return debug;
	}

	public void setDebug(boolean debug) {
		this.debug = debug;
	}

/*	public List<GradChannelEntity> getChannels(){
		if(cacheDatas.get(CRAWL_CHANNEL) == null){
			IGradChannelApplication gradChannelApplication = InstanceFactory.getInstance(IGradChannelApplication.class);
			try {
				cacheDatas.put(CRAWL_CHANNEL, gradChannelApplication.getEnableChannel());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return (List<GradChannelEntity>)cacheDatas.get(CRAWL_CHANNEL);
	}*/
	
//	
//	public boolean registerGroup(String groupName,long timeout){
//		boolean flag=false;
//		
//		if(!GROUP_DATA.containsKey(groupName)){
//			GROUP_DATA.put(groupName, new TaskGroup(groupName,timeout));
//			flag=true;
//		}
//		return flag;
//	}
//	
//	public Map<String,TaskGroup> getGroupData(){
//		return GROUP_DATA;
//	}
	

	
/*	public GradChannelEntity getChannel(Long id){
		if(id == null) return null;
		GradChannelEntity channel = null;
		for(GradChannelEntity e : getChannels()){
			if(id.longValue() == e.getId().longValue()){
				channel = e;
				break;
			}
		}
		return channel;
	}*/
	
	
}
